﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using IntrepidGame.CollisionObjects;
using IntrepidGameLibrary;

namespace IntrepidGame.CollisionObjects.ShipClasses
{
    public struct Attribute
    {
        int Value;
        float GrowthRate;
    }

    public struct TempAttribute
    {
        int TurnsLeft;
        int TempChange;
    }

    public class Ship : CollisionObject
    {
        #region Variables Region

        string shipName;

        // Atrributes are as follows:
        /*-- "Movement" , "Health" , "Power" , "Shield" , "Evasion" , "Luck" , "Level" --*/
        Dictionary<string, Attribute> attributes;
        Dictionary<string, TempAttribute> tempAttributes;

        /*for now*/
        public int maxMovement = 250;
        public int curMovement = 250;
        public List<VertexPositionColor> movementPathLinear;
        public List<int> movementMagnitudes;
        public Boolean ShipActive = true;

        public Texture2D MovementTexture;
        /*add properties later*/

        #endregion

        BasicEffect basicEffect;

        public Ship()
            : base()
        {
        }

        #region Override Functions

        public override bool CollisionWithObject(CollisionObject obj)
        {
            return base.CollisionWithObject(obj);
        }

        public override void LoadContent(Game1 gameRef, string imagePath, Rectangle source, Rectangle dest, Color tint)
        {
            base.LoadContent(gameRef, imagePath, source, dest, tint);

            movementPathLinear = new List<VertexPositionColor>();
            movementMagnitudes = new List<int>();
            ClearMovementPoints();

            //Used to draw lines
            basicEffect = new BasicEffect(GameRef.GraphicsDevice);
            basicEffect.VertexColorEnabled = true;
            basicEffect.Projection = Matrix.CreateOrthographicOffCenter(0, GameRef.screenRectangle.Width, 
                GameRef.screenRectangle.Height, 0, 0, 1);   

            MovementTexture = GameRef.Content.Load<Texture2D>("GUI/movementRing");
        }

        public override void Update(GameTime gameTime)
        {
            if (ShipActive)
            {
                if (InputHandler.MouseState.LeftButton == ButtonState.Pressed &&
                    InputHandler.PrevMouseState.LeftButton == ButtonState.Released)
                {
                    AddPointToMovement(new Point(InputHandler.MouseState.X, InputHandler.MouseState.Y));
                }

                if (InputHandler.MouseState.RightButton == ButtonState.Pressed &&
                    InputHandler.PrevMouseState.RightButton == ButtonState.Released)
                {
                    RemoveLastPoint();
                }
            }
            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            //if player's turn and selected and can move

            if (movementPathLinear.Count > 1)
            {
                basicEffect.CurrentTechnique.Passes[0].Apply();
                GameRef.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, movementPathLinear.ToArray(), 0, movementPathLinear.Count-1);
            }

            //All of this should be moved to the player class, and not stay in the ship class
            //draw circle only if player selected
            //Draw All Circles
            /*
            for (int i = 0; i < movementPathLinear.Count; i++)
            {
                Vector3 vpc = movementPathLinear[i].Position;
                Vector2 origin = new Vector2(vpc.X, vpc.Y);
                int movement = movementMagnitudes[i];

                Rectangle drawRectangle = new Rectangle((int)origin.X - movement, (int)origin.Y - movement, movement*2, movement*2);
                Color color = new Color(255.0f, (float)(movement / maxMovement) * 255.0f, 0.0f);

                spriteBatch.Draw(MovementTexture, drawRectangle, color);
            }
            */

            ///*// Draw only the lastest circle
            ///Comment all below and uncomment above for all circles
            Vector3 vpc = movementPathLinear[movementPathLinear.Count-1].Position;
            Vector2 origin = new Vector2(vpc.X, vpc.Y);
            int movement = movementMagnitudes[movementMagnitudes.Count-1];

            float colorConversion = ((float)movement / (float)maxMovement);
            colorConversion *= 255.0f;
            Rectangle drawRectangle = new Rectangle((int)origin.X - movement, (int)origin.Y - movement, movement*2, movement*2);
            Color color = new Color(255, (int)colorConversion, 0);

            spriteBatch.Draw(MovementTexture, drawRectangle, color);
             //*/

            base.Draw(spriteBatch);
        }

        #endregion

        public void AddPointToMovement(Point pos)
        {
            Point lastPoint = new Point((int)movementPathLinear[movementPathLinear.Count-1].Position.X, (int)movementPathLinear[movementPathLinear.Count-1].Position.Y);
            int movement = MoveDistance(pos, lastPoint);

            if (movement < movementMagnitudes[movementMagnitudes.Count - 1])
            {
                movementMagnitudes.Add(movementMagnitudes[movementMagnitudes.Count - 1] - movement);
                movementPathLinear.Add(new VertexPositionColor(new Vector3(pos.X, pos.Y, 0), Color.White));
            }

        }

        public void RemoveLastPoint()
        {
            int index = movementMagnitudes.Count - 1;
            if (index > 0)
            {
                movementMagnitudes.RemoveAt(index);
                movementPathLinear.RemoveAt(index);
            }
        }


        public void ClearMovementPoints()
        {
            movementPathLinear.Clear();
            movementMagnitudes.Clear();
            movementPathLinear.Add(new VertexPositionColor(new Vector3(Origin.X, Origin.Y, 0), Color.White));
            movementMagnitudes.Add(maxMovement);
        }

        private int MoveDistance(Point point1, Point point2)
        {
            int distance = (int)Math.Sqrt(Math.Pow((point1.X - point2.X), 2) + Math.Pow((point1.Y - point2.Y), 2));
            return distance;
        }
    }
}
